﻿(function (owner) {
    function undoRedoManager(){var r=false;var d=[];var p=0;this.d=function(){return d;};this.add=function(o){if(!o||d[p-1]==o)return false;d[p]=o;p++;d=d.slice(0,p);};this.canRedo=function(){if(p>=d.length){return false;}return true;};this.canUndo=function(){if(p<1){return false;}return true;};this.clear=function(){d=[];p=0;};this.undo=function(){if(p<1){return false;}p--;return d[p-1];};this.redo=function(){if(p>=d.length){return false;}p++;return d[p-1];};}

    function urPlugin() {
        this.urm = new undoRedoManager();
    }
    urPlugin.prototype = {
        init: function(editor) {
            var me = this;
            me.editor = editor;
                        
            function listener() {
                var c = me.getHtml();
                if (c && c != me.before) {
                    me.urm.add(c);
                }
            }
            
            $(editor.doc).focus(function() {
                me.before = me.getHtml();
            }).blur(listener).mouseout(listener).keyup(listener);
        },
        getHtml: function() {
            return this.editor.doc.body.innerHTML;
        },
        setHtml: function(html) {
            this.editor.doc.body.innerHTML = html;
        },
        undo: function() {
            if (!this.urm.canUndo()) {
                //alert('不能再撤销了');
                return;
            }
            var r = this.urm.undo();
            if (r)
                this.setHtml(r);
        },
        redo: function() {
            if (!this.urm.canRedo()) {
                //alert('不能再重做了');
                return;
            }
            var r = this.urm.redo();
            if (r)
                this.setHtml(r);
        }
    }

    var defaultInstance = {}, defaultUR = new urPlugin();
    var switchUploadMode = {/*切换上传模式*/
        singleUrl: '{editorRoot}upload.ashx',
        multiUrls: {
            link: '!{editorRoot}xheditor_plugins/multiupload/multiupload.html?uploadurl={editorRoot}upload.ashx%3Fimmediate%3D1&ext=附件文件(*.txt;*.rar;*.zip;*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx;*.pdf)',
            img: '!{editorRoot}xheditor_plugins/multiupload/multiupload.html?uploadurl={editorRoot}upload.ashx%3Fimmediate%3D1&ext=图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)',
            waterImg: '!{editorRoot}xheditor_plugins/multiupload/multiupload.html?uploadurl={editorRoot}upload.ashx%3Fimmediate%3D1%26watermark%3D1&ext=图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)',
            flash: '!{editorRoot}xheditor_plugins/multiupload/multiupload.html?uploadurl={editorRoot}upload.ashx%3Fimmediate%3D1&ext=Flash动画(*.swf)',
            flv: '!{editorRoot}xheditor_plugins/multiupload/multiupload.html?uploadurl={editorRoot}upload.ashx%3Fimmediate%3D1&ext=Flv视频(*.flv)',
            media: '!{editorRoot}xheditor_plugins/multiupload/multiupload.html?uploadurl={editorRoot}upload.ashx%3Fimmediate%3D1&ext=多媒体文件(*.wmv;*.avi;*.wma;*.wav;*.asf;*.mp3;*.mid)'
        },
        toggle: function (editor) {
            editor.isMultiUpload = !editor.isMultiUpload;
            switchUploadMode.setUploadUrl(editor);
        },
        setUploadUrl: function (editor) {
            var me = switchUploadMode;
            var isMulti = editor.isMultiUpload;
            editor.settings.upLinkUrl = isMulti ? me.multiUrls.link : me.singleUrl;
            editor.settings.upImgUrl = isMulti ? me.multiUrls.img : me.singleUrl;
            editor.settings.upWaterImgUrl = isMulti ? me.multiUrls.waterImg : me.singleUrl + '?immediate=1&watermark=1';
            editor.settings.upFlashUrl = isMulti ? me.multiUrls.flash : me.singleUrl;
            editor.settings.upFlvUrl = isMulti ? me.multiUrls.flv : me.singleUrl;
            editor.settings.upMediaUrl = isMulti ? me.multiUrls.media : me.singleUrl;
        },
        createPlugin: function () {
            return {
                c: 'plgIcon plgUpload',
                t: '切换上传模式',
                h: 1,
                e: function () {
                    var _this = this;
                    var umodetext = function () { return _this.isMultiUpload ? '切换为单文件上传' : '切换为多文件上传' };
                    var emodetext = function () { return defaultInstance.isAdvanceEdit ? '切换为简单模式' : '切换为高级模式' };
                    var dialog = $('<div>切换上传模式</div><div style="text-align:right;"><input type="button" id="xheUploadMode" /></div>\
                                           <div>切换编辑器模式</div><div style="text-align:right;"><input type="button" id="xheEditorMode" /></div>');
                    var jUploadMode = $('#xheUploadMode', dialog).val(umodetext());
                    var jEditorMode = $('#xheEditorMode', dialog).val(emodetext());
                    jUploadMode.click(function () {
                        switchUploadMode.toggle(_this);
                        jUploadMode.val(umodetext());
                        return false;
                    });
                    jEditorMode.click(function() {                        
                        _this.targetTextArea.xheditor(false);
                        defaultInstance.isAdvanceEdit = !defaultInstance.isAdvanceEdit;
                        configEditor(defaultInstance.targetTextArea);
                        if ($.browser.msie) {
                            $(defaultInstance.doc).queue('a', [defaultInstance.toggleSource,defaultInstance.toggleSource]).delay(100).dequeue('a').delay(100).dequeue('a');
                        }
                        return false;
                    });
                    _this.showDialog(dialog);
                }
            };
        }
    };

    function cleanWordEx(sHtml, deepClean) {
        //格式化
        sHtml = sHtml.replace(/(<link(?:\s+[^>]*?)?)\s+href\s*=\s*(["']?)\s*file:\/\/.+?\s*\2((?:\s+[^>]*?)?\s*\/?>)/ig, '');

        //区块标签清理
        sHtml = sHtml.replace(/<!--[\s\S]*?-->|<!(--)?\[[\s\S]+?\](--)?>|<style(\s+[^>]*?)?>[\s\S]*?<\/style>/ig, '');
        sHtml = sHtml.replace(/<\/?\w+:[^>]*>/ig, '');
        if (deepClean) sHtml = sHtml.replace(/<\/?(span)(\s+[^>]*?)?>/ig, '');

        //属性清理
        sHtml = sHtml.replace(/(<\w+(?:\s+[^>]*?)?)\s+class\s*=\s*(["']?)\s*mso.+?\s*\2((?:\s+[^>]*?)?\s*\/?>)/ig, "$1$3"); //删除所有mso开头的样式
        sHtml = sHtml.replace(/(<\w+(?:\s+[^>]*?)?)\s+lang\s*=\s*(["']?)\s*.+?\s*\2((?:\s+[^>]*?)?\s*\/?>)/ig, "$1$3"); //删除lang属性
        sHtml = sHtml.replace(/(<\w+(?:\s+[^>]*?)?)\s+align\s*=\s*(["']?)\s*left\s*\2((?:\s+[^>]*?)?\s*\/?>)/ig, "$1$3"); //取消align=left

        //样式清理
        sHtml = sHtml.replace(/<\w+(?:\s+[^>]*?)?(\s+style\s*=\s*(["']?)\s*([\s\S]*?)\s*\2)(?:\s+[^>]*?)?\s*\/?>/ig, function (all, attr, p, styles) {
            styles = $.trim(styles.replace(/\s*(mso-[^:]+:.+?|margin\s*:\s*0cm 0cm 0pt\s*|(text-align|font-variant|line-height)\s*:\s*.+?)(;|$)\s*/ig, ''));
            return all.replace(attr, deepClean ? '' : styles ? ' style="' + styles + '"' : '');
        });
        return sHtml;
    }

    function setTitlepic(v, fromUpload) {//设置标题图片
        var picel = $('#News_titlepic,.News_titlepic');
        if (picel.length > 0 && v) {
            var v2 = picel.val();
            if ((!fromUpload) || (!v2 && fromUpload)) {
                if (v[0] == '!') v = v.substring(1);
                var barr = v.split('/'), blen = barr.length - 1;
                var thumb = barr.slice(0, blen).join('/') + '/thumb_' + barr[blen];
                $(new Image()).load(function () {
                    picel.val(thumb); //先尝试小图
                }).error(function () {
                    picel.val(v); //小图挂了用大图
                }).attr('src', thumb);
            }
        }
    }
    
    var tableUtil = {
        tableProperty: function(cell, editorInstance) {
            var htmlTable='<div class="xheDialog"><div>宽度高度: <input type="text" id="xheTableWidth" style="width:40px;" value="200" /> x <input type="text" id="xheTableHeight" style="width:40px;" value="" /></div><div>边框大小: <input type="text" id="xheTableBorder" style="width:40px;" value="1" /></div><div>表格间距: <input type="text" id="xheTableCellSpacing" style="width:40px;" value="1" /></div><div>表格填充: <input type="text" id="xheTableCellPadding" style="width:40px;" value="1" /></div><div>对齐方式: <select id="xheTableAlign"><option selected="selected" value="">默认</option><option value="left">左对齐</option><option value="center">居中</option><option value="right">右对齐</option></select></div><div style="text-align:right;"><input type="button" id="xheSave" value="确定" /></div></div>';
			editorInstance.showModal('表格属性', htmlTable, 220,170);
			
            var jTable=$('div.xheModal'),jWidth=$('#xheTableWidth',jTable),jHeight=$('#xheTableHeight',jTable),jBorder=$('#xheTableBorder',jTable),jCellSpacing=$('#xheTableCellSpacing',jTable),jCellPadding=$('#xheTableCellPadding',jTable),jAlign=$('#xheTableAlign',jTable),jSave=$('#xheSave',jTable);
            var table = $(cell.offsetParent);
            jWidth.val(table.width());
            jHeight.val(table.height());
            jBorder.val(table.attr('border'));
            jCellSpacing.val(table.attr('cellspacing'));
            jCellPadding.val(table.attr('cellpadding'));
            jAlign.val(table.attr('align'));
            
            jSave.click(function(){
                table.width(jWidth.val())
                    .height(jHeight.val())
                    .attr('border', jBorder.val())
                    .attr('cellspacing', jCellSpacing.val())
                    .attr('cellpadding', jCellPadding.val())
                    .attr('align', jAlign.val());
                editorInstance.removeModal();
            });
        },
        insertCol : function(cell, offset) {
            var table = cell.offsetParent,
                index = cell.cellIndex + offset;
            for (var i = 0, len = table.rows.length; i < len; i++) {
                var newCell = table.rows[i].insertCell(index);
                newCell.innerHTML = '&nbsp;';
            }
        },
        insertColLeft : function(cell) {
            tableUtil.insertCol(cell, 0);
        },
        insertColRight : function(cell) {
            tableUtil.insertCol(cell, 1);
        },
        insertRow: function(cell, offset) {
            var row = cell.parentNode;
                table = row.offsetParent,
                newRow = table.insertRow(row.rowIndex + offset);
            for (var i = 0, len = row.cells.length; i < len; i++) {
                var newCell = newRow.insertCell(i);
                newCell.innerHTML = '&nbsp;';
            }
        },
        insertRowAbove : function(cell) {
            tableUtil.insertRow(cell, 0);
        },
        insertRowBelow : function(cell) {
            tableUtil.insertRow(cell, 1);
        },
        deleteCol : function(cell) {
            var table = cell.offsetParent;
            for (var i = 0, len = table.rows.length; i < len; i++) {
                table.rows[i].deleteCell(cell.cellIndex);
            }
            if (table.rows.length == 0) {
                tableUtil.deleteTable(cell);
            }
        },
        deleteRow : function(cell) {
            var row = cell.parentNode;
                table = row.offsetParent;
            table.deleteRow(row.rowIndex);
            if (table.rows.length == 0) {
                tableUtil.deleteTable(cell);
            }
        },
        deleteTable : function(cell) {
            var table = cell.offsetParent;
            table.parentNode.removeChild(table);
        }
    };

    var allPlugin = {
        WordFormat: { c: 'plgIcon plgBrush', t: '文字格式化', h: 1, e: function () {
            var _this = this;
            var jHtml = $('<div>清理区域: <select id="xheRegion"><option value="1">全部</option><option value="">选中区域</option></select></div><div>缩进模式: <select id="xheIdent"><option value="">无</option><option value="1" selected="selected">首行缩进</option><option value="2">清理缩进</option></select></div><div>清理链接: <input id="xheClearLink" type="checkbox" /></div><div>清理图片: <input id="xheClearImage" type="checkbox" /></div><div>清理空行: <input id="xheClearBlankLine" type="checkbox" /></div><div style="text-align:right;"><input type="button" id="xheSave" value="格式化" /></div>');
            var jSave = $('#xheSave', jHtml), jArea = $('#xheRegion', jHtml), jIdent = $('#xheIdent', jHtml), jCLink = $('#xheClearLink', jHtml), jCImg = $('#xheClearImage', jHtml), jCBlank = $('#xheClearBlankLine', jHtml);
            jSave.click(function () {
                var html;
                var clearAll = jArea.val();

                if (clearAll) {
                    html = _this.getSource();
                } else {
                    html = _this.getSelect();
                    if (!html)
                        alert("未选中文字");
                    html = _this.cleanHTML(html);
                    html = _this.formatXHTML(html);
                }

                html = cleanWordEx(html, true); //必须清理样式才能缩进
                html = html.replace(/(<table\s*((?!border)[^>])*)>/ig, '$1 border="1">'); //表格边框
                if (jCLink.attr('checked')) {
                    html = html.replace(/<\/?(a)(\s+[^>]*?)?>/ig, '');
                }
                if (jCImg.attr('checked')) {
                    html = html.replace(/<\/?(img)(\s+[^>]*?)?>/ig, '');
                }
                if (jCBlank.attr('checked')) {
                    html = html.replace(/(<(?:p|div|br)[^>]*>)((?:\s|&nbsp;|　|<br[^>]*>)*)(<(\/(?:p|div)|br[^>]*)>)/ig, '');
                }
                if (jIdent.val()) {//缩进处理
                    //html = html.replace(/(<\/?(?:p|br|span|div)[^>]*>)((?:\s|&nbsp;|　)*)/ig, '$1'); //p标记连着空格
                    //第一行纯文字特殊处理
                    html = html.replace(/^(?:\s|&nbsp;|　)+/, '');
                    if (!/^<[^>]+>/.test(html))
                        html = '　　' + html;
                    
                    var identReplaces = ['', '$1　　$2','$1$2'];
                    var jreplace = identReplaces[jIdent.val()];//1首行缩进 2清理缩进
                    
                    //对table外的p、br、div、hr标记增加缩进
                    var arr = html.split(/<table[^>]*>((?!<\/table>)[\s\S])*<\/table>/ig);
                    for (var i = 0; i < arr.length; i++) {
                        //html = html.replace(arr[i], arr[i].replace(/(<(?:p|br|div)[^>]*>)/ig, '$1　　'));
                        html = html.replace(arr[i], arr[i].replace(/(<(?:p|br|div|hr)[^>]*>)[\s|&nbsp;|　]*((?!<[^>]+>)[^<>]+)/ig, jreplace));
                    }
                }

                if (clearAll) {
                    _this.setSource(html);
                } else {
                    _this.pasteHTML(html);
                }
                _this.hidePanel();
                return false;
            });
            _this.showDialog(jHtml);
        }},
        PasteMethods: { c: 'xheIcon xheBtnPaste', t: '粘贴 (Ctrl+V)', h: 1, e: function () {
            var _this = this;
            var arrMenu = [{ s: '粘贴 (Ctrl+V)', v: 'Paste' }, { s: '无格式粘贴', v: 'Pastetext'}];
            _this.showMenu(arrMenu, function (v) { _this.loadBookmark(); _this.exec(v); });
        }},
        FontMethods: { c: 'plgIcon plgFontMethod', t: '常规字体操作', h: 1, e: function () {
            var _this = this;
            var arrMenu = [{ s: '粗体 (Ctrl+B)', v: 'Bold' }, { s: '斜体 (Ctrl+I)', v: 'Italic' }, { s: '下划线 (Ctrl+U)', v: 'Underline' }, { s: '删除线 (Ctrl+S)', v: 'Strikethrough' }, { s: '插入空格', v: '　' }, { s: '插入双空格', v: '　　'}];
            _this.showMenu(arrMenu, function (v) {
                _this.loadBookmark();
                if (v[0] == '　') _this.pasteHTML(v + _this.getSelect());
                else _this.exec(v);
            });
        }},
        Templates: { c: 'plgIcon plgTemplate', t: '选择模板', e: function () {
            var _this = this;
            _this.showIframeModal('选择模板', '{editorRoot}xheditor_plugins/templates/index.html', function (v) { _this.loadBookmark(); _this.pasteHTML(v); }, 500, 300);
        }},
        UploadModes: switchUploadMode.createPlugin(),
        TitleImage: { c: 'plgIcon plgTitleImage', t: '选择标题图片', h: 1, e: function () {
            var _this = this;
            var htmlImgs = '<div><strong>选择标题图片：</strong></div><div id="xheTitleImages" style="width:200px;height:170px;overflow:auto;text-align:center;">';
            var tmp = '';
            $('img', _this.doc).each(function () {
                var w = $(this).width(), h = $(this).height(), s = $(this).attr('_xhe_src');
                w = w > 80 ? 80 : w;
                h = h > 80 ? 80 : h;
                tmp += '<img style="width:' + w + 'px;height:' + h + 'px;cursor:pointer;margin:1px;border:1px solid #ccc" src="' + s + '" onclick="setTitlepic(\'' + s + '\');" />';
            });
            htmlImgs += (tmp || '没有可选的图片') + '</div>';
            _this.showDialog(htmlImgs);
        }},
        Flv: { c: 'btnFlv', t: '插入Flv视频', h: 1, e: function () {
            var _this = this;
            var htmlFlv = '<div>插入Flv视频: <input type="text" id="xheFlvUrl" value="http://" class="xheText" /></div><div>宽度高度: <input type="text" id="xheFlvWidth" style="width:40px;" value="480" /> x <input type="text" id="xheFlvHeight" style="width:40px;" value="400" /></div><div style="text-align:right;"><input type="button" id="xheSave" value="确定" /></div>';
            var jFlv = $(htmlFlv), jUrl = $('#xheFlvUrl', jFlv), jWidth = $('#xheFlvWidth', jFlv), jHeight = $('#xheFlvHeight', jFlv), jSave = $('#xheSave', jFlv);
            if (_this.settings.upFlvUrl) _this.uploadInit(jUrl, _this.settings.upFlvUrl, "flv");
            jSave.click(function () {
                _this.loadBookmark();
                _this.pasteHTML('<embed type="application/x-shockwave-flash" src="xheditor/xheditor_plugins/mediaplayer/player.swf" wmode="opaque" allowscriptaccess="always" allowfullscreen="true" quality="high" bgcolor="#ffffff" width="' + jWidth.val() + '" height="' + jHeight.val() + '" flashvars="file=' + jUrl.val() + '" />');
                _this.hidePanel();
                return false;
            });
            _this.showDialog(jFlv);
        }},
        WaterMarkImage: { c: 'plgIcon plgWaterImage', t: '上传图片并添加水印', h: 1, e: function () {
            var _this = this;
            var htmlWMImg = '<div><strong>上传图片并添加水印:</strong></div><div>图片文件: <input type="text" id="xheImgUrl" class="xheText" style="width:102px;" /></div><div>宽度高度: <input type="text" id="xheImgWidth" style="width:40px;" /> x <input type="text" id="xheImgHeight" style="width:40px;" /></div><div style="text-align:right;"><input type="button" id="xheSave" value="确定" /></div><div style="color:red;">\u652F\u6301\u6587\u4EF6\u7C7B\u578B\uFF1A'+_this.settings.upImgExt+'</div>';
            var jWMImg = $(htmlWMImg), jUrl = $('#xheImgUrl', jWMImg), jWidth = $('#xheImgWidth', jWMImg), jHeight = $('#xheImgHeight', jWMImg), jSave = $('#xheSave', jWMImg);
            if (_this.settings.upWaterImgUrl) _this.uploadInit(jUrl, _this.settings.upWaterImgUrl, _this.settings.upImgExt);
            jSave.click(function () {
                _this.loadBookmark();
                var url = jUrl.val();
                if (url !== '' && url !== 'http://') {
                    var aUrl = url.split(' '), sWidth = jWidth.val(), sHeight = jHeight.val();
                    if (aUrl.length > 1) {//批量插入
                        var sTemplate = '<img src="xhe_tmpurl"', sImg, arrImg = [];
                        if (sWidth !== '') sTemplate += ' width="' + sWidth + '"';
                        if (sHeight !== '') sTemplate += ' height="' + sHeight + '"';
                        sTemplate += ' />';
                        for (var i in aUrl) {
                            url = aUrl[i];
                            if (url !== '') {
                                url = url.split('||');
                                sImg = sTemplate;
                                sImg = sImg.replace('xhe_tmpurl', url[0]);
                                if (url[1]) sImg = '<a href="' + url[1] + '" target="_blank">' + sImg + '</a>'
                                arrImg.push(sImg);
                            }
                        }
                        _this.pasteHTML(arrImg.join('&nbsp;'));
                    }
                    else if (aUrl.length === 1) {//单URL模式
                        url = aUrl[0];
                        if (url !== '') {
                            url = url.split('||');
                            var sImg = '<img src="' + url[0] + '" _xhe_src="' + url[0] + '"';
                            if (sWidth !== '') sImg += ' width="' + sWidth + '"';
                            if (sHeight !== '') sImg += ' height="' + sHeight + '"';
                            sImg += ' />';
                            _this.pasteHTML(sImg);
                        }
                    }
                }
                _this.hidePanel();
                return false;
            });
            _this.showDialog(jWMImg);
        }},
        AlignLeft: { c: 'plgIcon plgTextLeft', t: '左对齐', e: function () {
            this._exec('justifyleft');
        }},
        AlignCenter: { c: 'plgIcon plgTextCenter', t: '居中', e: function () {
            this._exec('justifycenter');
        }},
        AlignRight: { c: 'plgIcon plgTextRight', t: '右对齐', e: function () {
            this._exec('justifyright');
        }},
        PageBreak: { c: 'plgIcon plgPageBreak', t: '插入分页符', e: function () {
            this.pasteHTML('<hr class="PageBreak" />');
        }},
        UndoAct: { c: 'plgIcon plgUndo', t: '撤销', e: function() {
            //this._exec('Undo');
            defaultUR.undo();
        }},
        RedoAct: { c: 'plgIcon plgRedo', t: '重做', e: function() {
            //this._exec('Redo');
            defaultUR.redo();
        }},
        TableEx: { c: 'plgIcon plgTable', t: '表格编辑', h: 1, e: function () {
            var _this = this;
            var arrMenu = [{s: '表格属性', v: 'tableProperty'}, { s: '左侧插入列', v: 'insertColLeft'}, { s: '右侧插入列', v: 'insertColRight'}, { s: '上方插入行', v: 'insertRowAbove'}, { s: '下方插入行', v: 'insertRowBelow'}, { s: '删除列', v: 'deleteCol'}, { s: '删除行', v: 'deleteRow'}, { s: '删除表格', v: 'deleteTable'}];
            _this.showMenu(arrMenu, function (v) {
                _this.loadBookmark();
                var el = _this.getParent();
                if (el.attr('tagName').toLowerCase() == 'td') {
                    tableUtil[v](el[0], _this);
                } else if ((el = el.closest('td')).length > 0) {
                    tableUtil[v](el[0], _this);
                }
            });
        }}
    };
    
    var simpleTools = {tools:'SelectAll,Cut,Copy,PasteMethods,Removeformat,WordFormat,|,FontSize,Fontface,FontMethods,FontColor,BackColor,|,AlignLeft,AlignCenter,AlignRight,|,Link,Unlink,Img,TitleImage,Flash,Table,Templates,UploadModes,|,Source'};
    var advanceTools = {tools:'UndoAct,RedoAct,|,SelectAll,Cut,Copy,PasteMethods,Removeformat,WordFormat,|,FontSize,Fontface,FontMethods,FontColor,BackColor,|,AlignLeft,AlignCenter,AlignRight,|,Link,Unlink,Img,TitleImage,WaterMarkImage,Flash,Table,TableEx,Templates,PageBreak,UploadModes,|,Source'};
    var defaultConfig = {
        plugins: allPlugin,
        upLinkExt: "txt,rar,zip,doc,docx,xls,xlsx,ppt,pptx,pdf,tif,tiff",
        upImgExt: "jpg,jpeg,gif,png,bmp",
        upFlashExt: "swf",
        upMediaExt: "wmv,avi,wma,wav,asf,mp3,mid",
        wordDeepClean: false,
        /*loadCSS: '/css/css.css',取消此注释用以加载前台样式*/
        /*background: 'url(/images/wordbg.gif)',取消此注释用以加入背景图片*/
        onUpload: function (imgs) {
            if (imgs.length > 0) {
                var isrc = imgs[0].url;
                if (/\.(jpg|jpeg|gif|png|bmp)$/.test(isrc))
                    setTitlepic(isrc , true);
                else
                  $('#xheLinkText').val(/*'附件：' + */isrc.replace(/.*\/([^/]+)\.\w+$/,'$1'));
            }
        }
    };

    owner.configEditor = function (element) {
        var edConfig = $.extend(defaultConfig, defaultInstance.isAdvanceEdit ? advanceTools : simpleTools);
        var editor = element.xheditor(defaultConfig);
        editor.targetTextArea = element;
        editor.isAdvanceEdit = defaultInstance.isAdvanceEdit;
        switchUploadMode.setUploadUrl(editor);
        defaultUR.init(editor);//undo redo
        defaultInstance = null;//destory instance
        defaultInstance = editor;
        return editor;
    };
    owner.setTitlepic = setTitlepic;
})(window);